package com.yhz.openapi.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhz.openapi.common.ResponseResult;
import com.yhz.openapi.common.ResultTable;
import com.yhz.openapi.dao.RoleMapper;
import com.yhz.openapi.entity.Menu;
import com.yhz.openapi.entity.Role;
import com.yhz.openapi.services.RoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServicesImpl implements RoleServices {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public ResultTable getRolePageWithName(Integer page, Integer limit, String name) {

        PageHelper.startPage(page, limit);
        //发送查询语句
        List<Role> roles = roleMapper.getRoleWithName(name);
        PageInfo pi = new PageInfo(roles);
        return new ResultTable(0, "", (int) pi.getTotal(), pi.getList());
    }

    @Override
    public ResponseResult updateStatus(Integer id, Integer status) {
        if (id > 0) {
            int i = roleMapper.updateByPrimaryKeySelective(new Role(id, status));
            if (i>0) {
                return ResponseResult.getSuccess("状态修改成功！");
            }
        }
        return ResponseResult.getFail("状态修改失败！");
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleId) {

       List<Integer> roles= roleMapper.getRoleMenu(roleId);
        return roles;
    }

    @Override
    public ResponseResult assignMenu(Integer roleId, Integer[] menuIds) {
        //1.先删除中间表中所有的roleId的数据
        roleMapper.delMenuByRoleId(roleId);
        //2.批量新增menuId数据中的数据
        // mybatis的批量处理
        roleMapper.insertMenus(roleId,menuIds);
        return ResponseResult.getSuccess();
    }
}
